<html>
<head><meta charset="utf-8"><title>Arbitrary bit-width integers · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html">Arbitrary bit-width integers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245880910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245880910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245880910">(Jul 13 2021 at 20:15)</a>:</h4>
<p>It doesn't look like arbitrary bit-width integers have ever been discussed for Rust. Has anyone considered adding them to Rust? I think they have some nice applications in some situations although often niche. LLVM supports them and I think they might have some performance advantages in some cases.</p>



<a name="245881396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245881396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245881396">(Jul 13 2021 at 20:19)</a>:</h4>
<p>what perf advantages do they have?</p>



<a name="245882312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245882312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245882312">(Jul 13 2021 at 20:28)</a>:</h4>
<p>They've been thoroughly discussed: <a href="https://github.com/rust-lang/rfcs/pull/2581">https://github.com/rust-lang/rfcs/pull/2581</a></p>



<a name="245882372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245882372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245882372">(Jul 13 2021 at 20:28)</a>:</h4>
<p>So depending on the environment these may be software-emulated but I believe there is also cases where there might be some hardware support to some extent for the types and using them instead of handling overflow logic manually will most likely often result in better codegen. I'm discussing this right now with someone on a Zig server (Zig has arbitrary bit-width integers) and we found out that the custom bit-width integer works better than manually doing the branch. Here's an actual use case: Imagine you have a 4x4 grid of apps and you want to be able to select through it in all four directions. We also want to jump back to the first or last item if we reached the end of either side of the grid.<br>
So for this we could have <code>x</code> and <code>y</code> variables of type <code>u2</code> to store the selected grid cell in. <code>u2</code> offers a range of 0 to 3 and that fits for our 4x4 grid so we simply need the code to increment <code>x</code> and <code>y</code> depending on the direction key pressed. The overflow and underflow logic does not need to be manually handled with <code>if</code>s (to set the axis to <code>0</code> or <code>3</code> which is usually pretty ugly to do). The <code>u2</code> does that naturally and that's one performance advantage. Of course that's just one thing. Here's another thing I read: <a href="https://www.theregister.com/2020/04/24/llvm_project_adds_support_for/">https://www.theregister.com/2020/04/24/llvm_project_adds_support_for/</a><br>
For FPGAs it's apparently useful too</p>



<a name="245883053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883053">(Jul 13 2021 at 20:31)</a>:</h4>
<p>I am very much in support of adding generic integers, I'm currently writing a Rust-based Hardware Description Language, and I had to write my own generic integer types: <a href="https://salsa.debian.org/Kazan-team/rust-hdl/-/blob/master/src/values/integer.rs">https://salsa.debian.org/Kazan-team/rust-hdl/-/blob/master/src/values/integer.rs</a></p>



<a name="245883096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883096">(Jul 13 2021 at 20:31)</a>:</h4>
<p>wouldn't arithmetic on them be lowered to <code>(a + b) &amp; MASK</code> in any case?</p>



<a name="245883106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883106">(Jul 13 2021 at 20:31)</a>:</h4>
<p><span class="user-mention" data-user-id="229517">@Jacob Lifshay</span>  Hmm, I see, I will give that a read. It seems to talk exclusively about custom bit sizes right? What about custom integer ranges? You see, if I wanted my menu grid to be 5x5 instead, I wouldn't have any fitting integer size available to choose from, the next one would be <code>u3</code> where the range goes up to 7, which is too big. I just read about this issue for Zig where they want custom <em>ranges</em>, not just <em>bits</em>: <a href="https://github.com/ziglang/zig/issues/3806">https://github.com/ziglang/zig/issues/3806</a><br>
Has that ever been discussed for Rust?</p>



<a name="245883237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883237">(Jul 13 2021 at 20:32)</a>:</h4>
<p>yes</p>



<a name="245883299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883299">(Jul 13 2021 at 20:32)</a>:</h4>
<p>Can you link an issue? Is it that issue?</p>



<a name="245883315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883315">(Jul 13 2021 at 20:33)</a>:</h4>
<p>for example here <a href="https://github.com/rust-lang/rfcs/issues/671">https://github.com/rust-lang/rfcs/issues/671</a></p>



<a name="245883359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883359">(Jul 13 2021 at 20:33)</a>:</h4>
<p>Oh, but it doesn't seem like <a href="https://github.com/rust-lang/rfcs/pull/2581">https://github.com/rust-lang/rfcs/pull/2581</a> is entirely dead, it has some discussion within in the last 26 days so maybe there is still hope</p>



<a name="245883490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883490">(Jul 13 2021 at 20:34)</a>:</h4>
<p>Very nice to see</p>



<a name="245883700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883700">(Jul 13 2021 at 20:36)</a>:</h4>
<p>yeah, it's postponed due to waiting on const-generics: <a href="https://github.com/rust-lang/rfcs/pull/2581#issuecomment-435233740">https://github.com/rust-lang/rfcs/pull/2581#issuecomment-435233740</a></p>



<a name="245883964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245883964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bot+ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245883964">(Jul 13 2021 at 20:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Arbitrary.20bit-width.20integers/near/245883096">said</a>:</p>
<blockquote>
<p>wouldn't arithmetic on them be lowered to <code>(a + b) &amp; MASK</code> in any case?</p>
</blockquote>
<p>improved data locality can be a solid win certain situations because Modern CPU &gt; IO speed / Memory bandwidth</p>
<p>packing structs to single usize is something I intend to explore after const eval &amp; generics land beta.</p>
<p>Language wise, <code>let x = 1u4;</code> being valid code would be nice.</p>



<a name="245884041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245884041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245884041">(Jul 13 2021 at 20:39)</a>:</h4>
<p>Even if LLVM might still just pad those custom integer bit widths to natural ones (8, 16, 32 et al), it can potentially still give great hints to LLVM about the boundaries and allow it to do some more optimizations</p>



<a name="245884087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245884087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245884087">(Jul 13 2021 at 20:39)</a>:</h4>
<p>yeah, but that's another can of worms</p>



<a name="245884189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245884189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245884189">(Jul 13 2021 at 20:40)</a>:</h4>
<p>it would be cool if data could be packed into bitfields somehow</p>



<a name="245884839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Arbitrary%20bit-width%20integers/near/245884839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Arbitrary.20bit-width.20integers.html#245884839">(Jul 13 2021 at 20:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/122651-general/topic/Arbitrary.20bit-width.20integers/near/245883096">said</a>:</p>
<blockquote>
<p>wouldn't arithmetic on them be lowered to <code>(a + b) &amp; MASK</code> in any case?</p>
</blockquote>
<p>only for unsigned ints or cases where the sign-extended bits aren't needed, signed ints would lower to a sign-extension</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>